"""
xls fields: NAME,TEL,URL,ADRESS1,ADDRESS2,CITY,ZIP,XCOORD,YCOORD
"""
import shapefile
import xlrd

# 1.打开 xls 电子表格
xls = xlrd.open_workbook("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_GEO.xls")
sheet = xls.sheet_by_index(0)

# 2.打开 shapefile 文件写者器
w = shapefile.Writer("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_XLS", shapeType=shapefile.POINT)

# 3.将数据从表格中复制到 shp 文件
#   读取第一行表头信息
for i in range(sheet.ncols):
    w.field(str(sheet.cell(0, i).value), "C", 40)
#   读取内容
for i in range(sheet.nrows):
    if i == 0:
        continue
    values = []
    for j in range(sheet.ncols):
        values.append(sheet.cell(i, j).value)
    w.record(*values)
    # 从最后两列获取经纬度
    w.point(float(values[-2]), float(values[-1]))


w.close()

# 验证 shp
r = shapefile.Reader("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_XLS")
print(r)
print(r.record(129))
r.close()
